
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IInformationBo;
import com.wyzz.hr.dao.IInformationDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Infoclass;
import com.wyzz.hr.pojo.Information;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.Pager;


public class InformationBoImpl implements IInformationBo {

	public InformationBoImpl() {
	}

	public IInformationDAO getInformationDAO() {
		return informationDAO;
	}

	public void setInformationDAO(IInformationDAO informationDAO) {
		this.informationDAO = informationDAO;
	}

	public boolean deleteInfo(Information info) {
		if (info == null)
			return true;
		String infoName = info.getInfoFileName();
		if (infoName != null && infoName.trim().length() > 0)
			FileOperate
					.deleteFile("sys.information.file.path", infoName.trim());
		String imgName = info.getInfoPicName();
		if (imgName != null && imgName.trim().length() > 0) {
			FileOperate.deleteFile("sys.information.image.path", imgName);
			imgName = imgName.replaceAll("_s", "");
			FileOperate.deleteFile("sys.information.image.path", imgName);
		}
		informationDAO.deleteObject(info);
		return true;
	}

	public Infoclass getInfoclass(String classId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Infoclass.class);
		detachedCriteria.add(Restrictions.eq(Infoclass.PROP_ID, classId));
		detachedCriteria.addOrder(Order.asc(Infoclass.PROP_INFOCLASS_SORT_ID));
		List list = informationDAO.findByCriteria(detachedCriteria);
		if (list == null || list.size() != 1)
			return null;
		else
			return (Infoclass) list.get(0);
	}

	public List getListbyClass(String infoid, String empId) {
		if (infoid == null)
			return new ArrayList();
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Information.class);
		detachedCriteria.setFetchMode("infoClass", FetchMode.JOIN);
		detachedCriteria.createCriteria("infoClass").add(
				Restrictions.eq("id", infoid));
		detachedCriteria.add(Restrictions.eq("infoStatus", Integer.valueOf(1)));
		detachedCriteria.addOrder(Order.desc("infoLastChangeTime"));
		if (empId != null) {
			String fetch[] = { "empDeptNo" };
			IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory
					.getBean("empBo");
			Employee employee = empBo.loadEmp(empId, fetch);
			detachedCriteria.add(Restrictions.disjunction().add(
					Restrictions.isNull(Information.PROP_INFO_TAG)).add(
					Restrictions.like(Information.PROP_INFO_TAG,
							"%\u6240\u6709\u90E8\u95E8%")).add(
					Restrictions.like(Information.PROP_INFO_TAG,
							(new StringBuilder()).append("%,")
									.append(
											employee.getEmpDeptNo()
													.getDepartmentName())
									.append(",%").toString())));
		}
		List result = informationDAO.findByCriteria(detachedCriteria, 3, 1);
		return result;
	}

	public List getListbyClassId(String classId, Pager page, String searchKey,
			Userinfo user, Integer infoStatus) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Information.class);
		detachedCriteria.setFetchMode("infoClass", FetchMode.JOIN);
		detachedCriteria.setFetchMode("infoCreateBy", FetchMode.JOIN);
		if (searchKey != null && searchKey.trim().length() > 0)
			detachedCriteria.add(Restrictions.or(Restrictions.like("infoTitle",
					(new StringBuilder()).append("%").append(searchKey).append(
							"%").toString()), Restrictions
					.or(Restrictions.like("infoBreif", (new StringBuilder())
							.append("%").append(searchKey).append("%")
							.toString()), Restrictions.or(Restrictions.like(
							"infoContent", (new StringBuilder()).append("%")
									.append(searchKey).append("%").toString()),
							Restrictions.like("infoTag", (new StringBuilder())
									.append("%").append(searchKey).append("%")
									.toString())))));
		if (classId != null && classId.length() > 0)
			detachedCriteria.createCriteria("infoClass").add(
					Restrictions.eq("id", classId));
		if (infoStatus != null && infoStatus.intValue() != 0)
			detachedCriteria.add(Restrictions.eq("infoStatus", infoStatus));
		if (user != null) {
			String fetch[] = { "empDeptNo" };
			IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory
					.getBean("empBo");
			Employee employee = empBo.loadEmp(user.getId(), fetch);
			detachedCriteria.add(Restrictions.or(Restrictions.like("infoTag",
					(new StringBuilder()).append("%,").append(
							employee.getEmpDeptNo().getDepartmentName())
							.append(",%").toString()), Restrictions.like(
					"infoTag", "%\u6240\u6709\u90E8\u95E8%")));
		}
		detachedCriteria.addOrder(Order.desc("infoLastChangeTime"));
		List result = informationDAO.findByCriteria(detachedCriteria);
		int size = result.size();
		int pageSize = Integer.valueOf(
				DatabaseSysConfigManager.getInstance().getProperty(
						"sys.split.pages")).intValue();
		page.init(size, pageSize);
		if (page.getOperate() != null
				&& "previous".equalsIgnoreCase(page.getOperate()))
			page.previous();
		if (page.getOperate() != null
				&& "next".equalsIgnoreCase(page.getOperate()))
			page.next();
		if (page.getOperate() != null
				&& "first".equalsIgnoreCase(page.getOperate()))
			page.first();
		if (page.getOperate() != null
				&& "last".equalsIgnoreCase(page.getOperate()))
			page.last();
		List returnList = new ArrayList();
		if (size == 0)
			return returnList;
		int endPage = size <= page.getCurrentPage() * pageSize ? size : page
				.getCurrentPage()
				* pageSize;
		for (int i = (page.getCurrentPage() - 1) * pageSize; i < endPage; i++)
			returnList.add(result.get(i));

		return returnList;
	}

	public List getObjects(Class clas, String fetch[]) {
		List result = informationDAO.getObjects(clas, fetch);
		return result;
	}

	public List getOrderInfos(String fieldName, String methods) {
		return informationDAO.exeHqlList((new StringBuilder()).append(
				"from Information order by ").append(fieldName).append(" ")
				.append(methods).toString());
	}

	public List insertInfo(Information info) {
		informationDAO.saveObject(info);
		return null;
	}

	public Information loadInfo(Object id, String fetch[]) {
		return (Information) informationDAO.loadObject(
				com.wyzz.hr.pojo.Information.class, id, fetch, new boolean[0]);
	}

	public Information loadOne(String oneId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Information.class);
		detachedCriteria.add(Restrictions.eq(Information.PROP_ID, oneId));
		List list = informationDAO.findByCriteria(detachedCriteria);
		if (list == null || list.size() != 1)
			return null;
		else
			return (Information) list.get(0);
	}

	public boolean updateInfo(Information info) {
		informationDAO.updateObject(info);
		return true;
	}

	public boolean saveObject(Information info) {
		try {
			List errors = new ArrayList();
			if (errors.size() < 1) {
				informationDAO.saveObject(info);
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public Date getLastMonthDate() {
		Calendar cal = Calendar.getInstance();
		cal.add(2, 0);
		cal.set(5, 0);
		return cal.getTime();
	}

	public Date getNextMonthDate() {
		Calendar cal = Calendar.getInstance();
		cal.add(2, 1);
		cal.set(5, 1);
		return cal.getTime();
	}

	public List getInfoClassBySortId() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Infoclass.class);
		detachedCriteria.addOrder(Order.asc(Infoclass.PROP_INFOCLASS_SORT_ID));
		List list = informationDAO.findByCriteria(detachedCriteria);
		return list;
	}

	private IInformationDAO informationDAO;
}
